package com.ruoyi.common.core.controller;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.model.ciModel.snapshot.SnapshotRequest;
import com.qcloud.cos.model.ciModel.snapshot.SnapshotResponse;
import com.qcloud.cos.region.Region;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.URLDecoder;
import java.util.Date;

/**
 * @author chengliang
 * @date 2022/8/24
 **/
@Component
public class HandleVideoSrc {
//    public static void main(String[] args) {
//        String  handle = HandleVideoSrc.handle("UigOr53mHagaHjwue9PQCmhU7WkmaAWd",
//                "AKID30oRr2ziDVdbMuENMCpJbfU7UcWAZliP",
//                "ap-beijing",
//                "yifeng-1308901251",
//                "https://yifeng-1308901251.cos.ap-beijing.myqcloud.com/upload/2022/08/22/0a16df0e-ce35-49b1-aced-e47a7e2f8ab8.mp4");
//        System.out.println(handle);
//    }
    public static String handle(String key,String id,String regionName,String bucketName,String filePath){
        BasicCOSCredentials cred = new BasicCOSCredentials(id,key );
        Region region = new Region(regionName);
        ClientConfig clientConfig = new ClientConfig(region);
        COSClient client = new COSClient(cred,clientConfig);

        //1.创建截图请求对象
        SnapshotRequest request = new SnapshotRequest();
        filePath = URLDecoder.decode(filePath);

        String[] file = filePath.split(".com");

//2.添加请求参数 参数详情请见api接口文档
        request.setBucketName(bucketName);
//        String fileName = file[file.length-1];
        request.getInput().setObject(file[file.length-1]);
        request.getOutput().setBucket(bucketName);
        request.getOutput().setRegion(regionName);
        request.getOutput().setObject("/videoTemp/"+ DateUtils.parseDateToStr("yyyy/MM/dd",new Date())+"/"+getRandomName());
        request.setTime("0");
        //3.调用接口,获取截图响应对象
        SnapshotResponse snapshotResponse = client.generateSnapshot(request);
        String src = "https://"+snapshotResponse.getOutput().getBucket()+".cos."+snapshotResponse.getOutput().getRegion()+".myqcloud.com"+snapshotResponse.getOutput().getObject();
        return src;
    }

    public static String getRandomName(){
        String uuidFileName= UUID.randomUUID().toString()+".jpg";
        return uuidFileName;
    }

}
